package com.diyunapp.xilinapp.me;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EmailCerActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.email_cer_edt)
    EditText emailCerEdt;
    @Bind(R.id.ceremail_right_away)
    TextView ceremailRightAway;
    private String username;
    private View headView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_cer);
        ButterKnife.bind(this);
        headView = findViewById(R.id.emaice_menubar_inc);
        super.MenuWidget(headView);
        SharedPreferences userinfo=getSharedPreferences("user_info",MODE_PRIVATE);
        username=userinfo.getString("username","");
    }
    @OnClick({ R.id.go_back, R.id.ceremail_right_away})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                nextAct(AccountSafeActivity.class);
                break;
            case R.id.ceremail_right_away:
                if(emailCerEdt.getText().toString().trim()==""||emailCerEdt.getText().toString().trim().equals("")){
                    Toast.makeText(this,"邮箱为空",Toast.LENGTH_SHORT).show();
                }else{
                        //弹出dialog
//                    initBandDlg("deweewrewr");
                    getState(username);

                }
                break;



        }
    }
    private void getBindData(String id) {
        String url=""+MainActivity.url+"act=goods&op=find_mobile_email&member_id="+ MainActivity.myLogin.getMemberid()+"";
//        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=find_mobile_email&member_id="+id+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject recordObject = resultObject.optJSONObject("data");


                                String member_email = recordObject.optString("member_email");
                                String member_mobile = recordObject.optString("member_mobile");
                                int member_email_bind = recordObject.optInt("member_email_bind");
                                int member_mobile_bind = recordObject.optInt("member_mobile_bind");
                                String[] m = new String[0];
                                if (member_email_bind == 1) {
                                    m = new String[]{member_email};
                                }



                            } else {
                                toast("当前暂无数据");

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");

                    }
                });
    }
    private void getState(String name) {
//        String url="http://www.xilingou.com/mobile/index.php?act=goods&op=send_bind_email&username=gagaha&email=1695219074@qq.com";

        String url=""+MainActivity.url+"act=goods&op=send_bind_email&username="+ MainActivity.myLogin.getUsername()+"&email="+emailCerEdt.getText().toString().trim()+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(10000);
        http.configTimeout(10 * 1000);// 连接超时  //指的是连接一个url的连接等待时间。
        http.configSoTimeout(10 * 1000);
        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            String  information = resultObject.optString("data");
                            if (result == 200) {

                                initBandDlg(information);
                            } else {
                                toast(information);


                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {

                        toast("请求失败");

                    }
                });
    }

    public void initBandDlg(final String m){
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        final View getmoney = inflater.inflate(R.layout.dlg_getmoney, null);
        TextView yes = ButterKnife.findById(getmoney,R.id.yes);
        TextView information = ButterKnife.findById(getmoney,R.id.information);
        information.setText(m);

        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 2. Chain together various setter methods to set the dialog characteristics
        builder.setView(getmoney);
        // 3. Get the AlertDialog from create()
        final AlertDialog dialog = builder.create();
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialog.dismiss();
            }
        });
        dialog.show();
    }
    public void nextAct(Class act) {
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }
}
